package com.wyzz.hr.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import org.apache.commons.lang.StringUtils;

import com.wyzz.hr.pojo.Perfplanscore;

public class InterpreterForPerformance {

	public InterpreterForPerformance() {
	}

	public BigDecimal calculateValue(String formula)
			throws InterpreterException {
		if (StringUtils.isEmpty(formula))
			return BigDecimal.ZERO;
		setValidateFlag(0);
		try {
			double result = calculateFormula(formula);
			DecimalFormat df = new DecimalFormat("#.00");
			return new BigDecimal(df.format(result));
		} catch (InterpreterException ex) {
			throw new InterpreterException(ex.getMessage());
		}
	}

	public String formulaValidate(String formula) {
		formula = formula.replaceAll(" ", "");
		formula = formula.toUpperCase();
		if (StringUtils.isEmpty(formula))
			return "SUCC";
		setValidateFlag(1);
		try {
			calculateFormula(formula);
		} catch (InterpreterException ex) {
			return ex.getMessage();
		}
		return "SUCC";
	}

	private double calculateFormula(String formula) throws InterpreterException {
		double result = 0.0D;
		formula = removeBrackets(formula);
		if (StringUtils.isEmpty(formula))
			throw new InterpreterException("\u53C2\u6570\u4E3A\u7A7A\uFF01");
		int i = seperateFormula(formula);
		if (i > 0) {
			result = calculateFormula(formula.substring(0, i));
			double resultRight = calculateFormula(formula.substring(i + 1,
					formula.length()));
			result = calculateBasic090414(result, resultRight, formula
					.charAt(i));
			return result;
		}
		if (formula.startsWith("F") && formula.length() > 1
				&& formula.charAt(1) >= '1' && formula.charAt(1) <= '4')
			result = getFieldValue(formula).doubleValue();
		else if (formula.charAt(0) >= '0' && formula.charAt(0) <= '9'
				|| formula.charAt(0) == '-' || formula.charAt(0) == '.') {
			try {
				result = Double.parseDouble(formula);
			} catch (Exception e) {
				throw new InterpreterException((new StringBuilder()).append(
						"\u6570\u503C").append(formula).append(
						"\u89E3\u6790\u9519\u8BEF\uFF01").toString());
			}
		} else {
			if (formula.startsWith("IF") || formula.startsWith("?")) {
				if (formula.startsWith("?"))
					formula = formula.substring(1);
				else
					formula = formula.substring(2);
				if (formula.startsWith("(") && formula.endsWith(")")) {
					String subformula[] = splitFormula(formula.substring(1,
							formula.length() - 1), ',');
					if (subformula.length != 2 && subformula.length != 3)
						throw new InterpreterException(
								(new StringBuilder())
										.append("IF\u516C\u5F0F")
										.append(formula)
										.append(
												"\u53C2\u6570\u4E2A\u6570\u4E0D\u6B63\u786E\uFF01\n\u6B63\u786E\u683C\u5F0F\uFF1A\"IF(\u903B\u8F91\u8868\u8FBE\u5F0F, X1, X2)\"\uFF0C\u903B\u8F91\u8868\u8FBE\u5F0F\u4E3A\u771F\u53D6X1\u503C\uFF0C\u5426\u5219\u53D6X2\u503C\u3002X2\u5141\u8BB8\u4E3A\u7A7A\uFF01")
										.toString());
					if (getValidateFlag() == 1) {
						double resultLeft = calculateFormula(subformula[1]);
						double resultRight;
						if (subformula.length == 2)
							resultRight = 0.0D;
						else
							resultRight = calculateFormula(subformula[2]);
						if (calculateBool090409(subformula[0]).booleanValue())
							return resultLeft;
						else
							return resultRight;
					}
					if (calculateBool090409(subformula[0]).booleanValue())
						return calculateFormula(subformula[1]);
					if (subformula.length == 2)
						return 0.0D;
					else
						return calculateFormula(subformula[2]);
				} else {
					throw new InterpreterException(
							(new StringBuilder())
									.append("IF\u516C\u5F0F")
									.append(formula)
									.append(
											"\u8BED\u6CD5\u4E0D\u6B63\u786E\uFF01\n\u6B63\u786E\u683C\u5F0F\uFF1A\"IF(\u903B\u8F91\u8868\u8FBE\u5F0F, X1, X2)\"\uFF0C\u903B\u8F91\u8868\u8FBE\u5F0F\u4E3A\u771F\u53D6X1\u503C\uFF0C\u5426\u5219\u53D6X2\u503C\u3002X2\u5141\u8BB8\u4E3A\u7A7A\uFF01")
									.toString());
				}
			}
			if (formula.startsWith("ROUNDUP")) {
				formula = formula.substring(7);
				if (formula.startsWith("(") && formula.endsWith(")")) {
					int indexsplit = formula.lastIndexOf(",");
					if (indexsplit != -1)
						try {
							result = calculateFormula(formula.substring(1,
									indexsplit));
							int roundupFactor = Integer.parseInt(formula
									.substring(indexsplit + 1,
											formula.length() - 1));
							return result = MyTools.ceil(result, roundupFactor);
						} catch (Exception e) {
							throw new InterpreterException(
									(new StringBuilder())
											.append("ROUNDUP\u516C\u5F0F")
											.append(formula)
											.append(
													"\u8BED\u6CD5\u4E0D\u6B63\u786E\uFF01\n\u6B63\u786E\u683C\u5F0F\uFF1A\"ROUNDUP(F1,1)\"\uFF0C\u5982F1=2.14\uFF0C\u5411\u4E0A\u8FDB\u4E00\u4F4D\u5C0F\u6570\uFF0C\u7ED3\u679C\u4E3A2.2\uFF01")
											.toString());
						}
				}
				throw new InterpreterException(
						(new StringBuilder())
								.append("ROUNDUP\u516C\u5F0F")
								.append(formula)
								.append(
										"\u8BED\u6CD5\u4E0D\u6B63\u786E\uFF01\n\u6B63\u786E\u683C\u5F0F\uFF1A\"ROUNDUP(F1,1)\"\uFF0C\u5982F1=2.14\uFF0C\u5411\u4E0A\u8FDB\u4E00\u4F4D\u5C0F\u6570\uFF0C\u7ED3\u679C\u4E3A2.2\uFF01")
								.toString());
			}
			if (formula.startsWith("ROUNDDOWN")) {
				formula = formula.substring(9);
				if (formula.startsWith("(") && formula.endsWith(")")) {
					int indexsplit = formula.lastIndexOf(",");
					if (indexsplit != -1)
						try {
							result = calculateFormula(formula.substring(1,
									indexsplit));
							int roundupFactor = Integer.parseInt(formula
									.substring(indexsplit + 1,
											formula.length() - 1));
							return result = MyTools
									.floor(result, roundupFactor);
						} catch (Exception e) {
							throw new InterpreterException(
									(new StringBuilder())
											.append("ROUNDDOWN\u516C\u5F0F")
											.append(formula)
											.append(
													"\u8BED\u6CD5\u4E0D\u6B63\u786E\uFF01\n\u6B63\u786E\u683C\u5F0F\uFF1A\"ROUNDDOWN(F1,1)\"\uFF0C\u5982F1=2.15\uFF0C\u4EC5\u4FDD\u7559\u4E00\u4F4D\u5C0F\u6570\uFF0C\u7ED3\u679C\u4E3A2.1\uFF01")
											.toString());
						}
				}
				throw new InterpreterException(
						(new StringBuilder())
								.append("ROUNDDOWN\u516C\u5F0F")
								.append(formula)
								.append(
										"\u8BED\u6CD5\u4E0D\u6B63\u786E\uFF01\n\u6B63\u786E\u683C\u5F0F\uFF1A\"ROUNDDOWN(F1,1)\"\uFF0C\u5982F1=2.15\uFF0C\u4EC5\u4FDD\u7559\u4E00\u4F4D\u5C0F\u6570\uFF0C\u7ED3\u679C\u4E3A2.1\uFF01")
								.toString());
			}
			if (formula.startsWith("ROUND")) {
				formula = formula.substring(5);
				if (formula.startsWith("(") && formula.endsWith(")")) {
					int indexsplit = formula.lastIndexOf(",");
					if (indexsplit != -1)
						try {
							result = calculateFormula(formula.substring(1,
									indexsplit));
							int roundupFactor = Integer.parseInt(formula
									.substring(indexsplit + 1,
											formula.length() - 1));
							return result = MyTools
									.round(result, roundupFactor);
						} catch (Exception e) {
							throw new InterpreterException(
									(new StringBuilder())
											.append("ROUND\u516C\u5F0F")
											.append(formula)
											.append(
													"\u8BED\u6CD5\u4E0D\u6B63\u786E\uFF01\n\u6B63\u786E\u683C\u5F0F\uFF1A\"ROUND(F1,1)\"\uFF0C\u5982F1=2.15\uFF0C\u56DB\u820D\u4E94\u5165\u4E00\u4F4D\u5C0F\u6570\uFF0C\u7ED3\u679C\u4E3A2.2\uFF01")
											.toString());
						}
				}
				throw new InterpreterException(
						(new StringBuilder())
								.append("ROUND\u516C\u5F0F")
								.append(formula)
								.append(
										"\u8BED\u6CD5\u4E0D\u6B63\u786E\uFF01\n\u6B63\u786E\u683C\u5F0F\uFF1A\"ROUND(F1,1)\"\uFF0C\u5982F1=2.15\uFF0C\u56DB\u820D\u4E94\u5165\u4E00\u4F4D\u5C0F\u6570\uFF0C\u7ED3\u679C\u4E3A2.2\uFF01")
								.toString());
			}
			if (formula.startsWith("CEILING")) {
				formula = formula.substring(7);
				if (formula.startsWith("(") && formula.endsWith(")")) {
					int indexsplit = formula.lastIndexOf(",");
					result = calculateFormula(formula.substring(1, indexsplit));
					if (indexsplit != -1)
						try {
							double ceilingFactor = Double.parseDouble(formula
									.substring(indexsplit + 1,
											formula.length() - 1));
							result = Math.ceil(result / ceilingFactor)
									* ceilingFactor;
							return result;
						} catch (Exception e) {
							throw new InterpreterException(
									(new StringBuilder())
											.append("CEILING\u516C\u5F0F")
											.append(formula)
											.append(
													"\u8BED\u6CD5\u4E0D\u6B63\u786E\uFF01\n\u6B63\u786E\u683C\u5F0F\uFF1A\"CEILING(F1,10)\"\uFF0C\u5982F1=101\uFF0C\u53D6\u6BD4F1\u5927\u3001\u8FD8\u80FD\u88AB10\u6574\u9664\u7684\u503C\uFF0C\u7ED3\u679C\u4E3A110\uFF01")
											.toString());
						}
				}
				throw new InterpreterException(
						(new StringBuilder())
								.append("CEILING\u516C\u5F0F")
								.append(formula)
								.append(
										"\u8BED\u6CD5\u4E0D\u6B63\u786E\uFF01\n\u6B63\u786E\u683C\u5F0F\uFF1A\"CEILING(F1,10)\"\uFF0C\u5982F1=101\uFF0C\u53D6\u6BD4F1\u5927\u3001\u8FD8\u80FD\u88AB10\u6574\u9664\u7684\u503C\uFF0C\u7ED3\u679C\u4E3A110\uFF01")
								.toString());
			}
			if (formula.startsWith("MAX")) {
				formula = formula.substring(3);
				if (formula.startsWith("(") && formula.endsWith(")")) {
					String subformula[] = splitFormula(formula.substring(1,
							formula.length() - 1), ',');
					if (subformula.length < 2)
						throw new InterpreterException(
								(new StringBuilder())
										.append("MAX\u516C\u5F0F")
										.append(formula)
										.append(
												"\u81F3\u5C11\u6709\u4E24\u4E2A\u4EE5\u4E0A\u53C2\u6570\uFF01\n\u6B63\u786E\u683C\u5F0F\uFF1A\"MAX(F1,F2,F3)\"\uFF0C\u8868\u793A\u53D6F1\u3001F2\u3001F3\u4E2D\u7684\u6700\u5927\u503C\uFF01")
										.toString());
					double resultMax = calculateFormula(subformula[0]);
					for (int j = 1; j < subformula.length; j++) {
						result = calculateFormula(subformula[j]);
						if (result > resultMax)
							resultMax = result;
					}

					return resultMax;
				} else {
					throw new InterpreterException(
							(new StringBuilder())
									.append("MAX\u516C\u5F0F")
									.append(formula)
									.append(
											"\u8BED\u6CD5\u4E0D\u6B63\u786E\uFF01\n\u6B63\u786E\u683C\u5F0F\uFF1A\"MAX(F1,F2,F3)\"\uFF0C\u8868\u793A\u53D6F1\u3001F2\u3001F3\u4E2D\u7684\u6700\u5927\u503C\uFF01")
									.toString());
				}
			}
			if (formula.startsWith("MIN")) {
				formula = formula.substring(3);
				if (formula.startsWith("(") && formula.endsWith(")")) {
					String subformula[] = splitFormula(formula.substring(1,
							formula.length() - 1), ',');
					if (subformula.length < 2)
						throw new InterpreterException(
								(new StringBuilder())
										.append("MIN\u516C\u5F0F")
										.append(formula)
										.append(
												"\u81F3\u5C11\u6709\u4E24\u4E2A\u4EE5\u4E0A\u53C2\u6570\uFF01\n\u6B63\u786E\u683C\u5F0F\uFF1A\"MIN(F1,F2,F3)\"\uFF0C\u8868\u793A\u53D6F1\u3001F2\u3001F3\u4E2D\u7684\u6700\u5C0F\u503C\uFF01")
										.toString());
					double resultMin = calculateFormula(subformula[0]);
					for (int j = 1; j < subformula.length; j++) {
						result = calculateFormula(subformula[j]);
						if (result < resultMin)
							resultMin = result;
					}

					return resultMin;
				} else {
					throw new InterpreterException(
							(new StringBuilder())
									.append("MIN\u516C\u5F0F")
									.append(formula)
									.append(
											"\u8BED\u6CD5\u4E0D\u6B63\u786E\uFF01\n\u6B63\u786E\u683C\u5F0F\uFF1A\"MIN(F1,F2,F3)\"\uFF0C\u8868\u793A\u53D6F1\u3001F2\u3001F3\u4E2D\u7684\u6700\u5C0F\u503C\uFF01")
									.toString());
				}
			}
			if (formula.startsWith("INT")) {
				formula = formula.substring(3);
				if (formula.startsWith("(") && formula.endsWith(")")) {
					result = calculateFormula(formula.substring(1, formula
							.length() - 1));
					return Math.floor(result);
				} else {
					throw new InterpreterException((new StringBuilder())
							.append("INT\u516C\u5F0F").append(formula).append(
									"\u8BED\u6CD5\u4E0D\u6B63\u786E\uFF01")
							.toString());
				}
			}
			if (formula.startsWith("SIGN")) {
				formula = formula.substring(4);
				if (formula.startsWith("(") && formula.endsWith(")")) {
					result = calculateFormula(formula.substring(1, formula
							.length() - 1));
					return Math.signum(result);
				} else {
					throw new InterpreterException((new StringBuilder())
							.append("SIGN\u516C\u5F0F").append(formula).append(
									"\u8BED\u6CD5\u4E0D\u6B63\u786E\uFF01")
							.toString());
				}
			}
			if (formula.startsWith("ABS")) {
				formula = formula.substring(3);
				if (formula.startsWith("(") && formula.endsWith(")")) {
					result = calculateFormula(formula.substring(1, formula
							.length() - 1));
					return Math.abs(result);
				} else {
					throw new InterpreterException((new StringBuilder())
							.append("ABS\u516C\u5F0F").append(formula).append(
									"\u8BED\u6CD5\u4E0D\u6B63\u786E\uFF01")
							.toString());
				}
			}
			if (formula.startsWith("MOD")) {
				formula = formula.substring(3);
				if (formula.startsWith("(") && formula.endsWith(")")) {
					int indexsplit = formula.lastIndexOf(",");
					if (indexsplit != -1)
						try {
							result = calculateFormula(formula.substring(1,
									indexsplit));
							double modFactor = Double.parseDouble(formula
									.substring(indexsplit + 1,
											formula.length() - 1));
							result -= Math.floor(result / modFactor)
									* modFactor;
							return result;
						} catch (Exception e) {
							throw new InterpreterException(
									(new StringBuilder())
											.append("MOD\u516C\u5F0F")
											.append(formula)
											.append(
													"\u8BED\u6CD5\u4E0D\u6B63\u786E\uFF01\n\u6B63\u786E\u683C\u5F0F\uFF1A\"MOD(F1,10)\"\uFF0C\u5982F1=1237\uFF0C\u7ED3\u679C\u4E3A7\uFF0C\u8868\u793A\u7528F1\u503C\u966410\u6765\u83B7\u53D6\u4F59\u6570\uFF01")
											.toString());
						}
				}
				throw new InterpreterException(
						(new StringBuilder())
								.append("MOD\u516C\u5F0F")
								.append(formula)
								.append(
										"\u8BED\u6CD5\u4E0D\u6B63\u786E\uFF01\n\u6B63\u786E\u683C\u5F0F\uFF1A\"MOD(F1,10)\"\uFF0C\u5982F1=1237\uFF0C\u7ED3\u679C\u4E3A7\uFF0C\u8868\u793A\u7528F1\u503C\u966410\u6765\u83B7\u53D6\u4F59\u6570\uFF01")
								.toString());
			} else {
				throw new InterpreterException((new StringBuilder()).append(
						"\u516C\u5F0F\u5B57\u7B26\u4E32").append(formula)
						.append("\u89E3\u6790\u9519\u8BEF\uFF01").toString());
			}
		}
		return result;
	}

	private Boolean calculateBool090409(String formula)
			throws InterpreterException {
		formula = removeBrackets(formula);
		boolean BoolResult = false;
		if (formula.indexOf('*') != -1) {
			String subformula[] = splitFormula(formula, '*');
			BoolResult = calculateBool090409(subformula[0]).booleanValue();
			for (int i = 1; i < subformula.length; i++) {
				if (subformula[i].startsWith("AND(")
						&& subformula[i].endsWith(")")) {
					BoolResult = BoolResult
							&& calculateBool090409(
									subformula[i].substring(4, subformula[i]
											.length() - 1)).booleanValue();
					continue;
				}
				if (subformula[i].startsWith("OR(")
						&& subformula[i].endsWith(")"))
					BoolResult = BoolResult
							|| calculateBool090409(
									subformula[i].substring(3, subformula[i]
											.length() - 1)).booleanValue();
				else
					throw new InterpreterException((new StringBuilder())
							.append("IF\u903B\u8F91\u8868\u8FBE\u5F0F").append(
									subformula[i]).append(
									"\u8BED\u6CD5\u4E0D\u6B63\u786E\uFF01")
							.toString());
			}

			return Boolean.valueOf(BoolResult);
		}
		if (formula.startsWith("NOT(") && formula.endsWith(")"))
			return Boolean.valueOf(!calculateBool090409(
					formula.substring(4, formula.length() - 1)).booleanValue());
		int symbolLength = 1;
		int symbolIndex = 0;
		String symbol;
		if (formula.indexOf("<=") > 0) {
			symbol = "<=";
			symbolLength = 2;
			symbolIndex = formula.indexOf("<");
		} else if (formula.indexOf(">=") > 0) {
			symbol = ">=";
			symbolLength = 2;
			symbolIndex = formula.indexOf(">");
		} else if (formula.indexOf("==") > 0) {
			symbol = "==";
			symbolLength = 2;
			symbolIndex = formula.indexOf("=");
		} else if (formula.indexOf("<>") > 0) {
			symbol = "<>";
			symbolLength = 2;
			symbolIndex = formula.indexOf("<");
		} else if (formula.indexOf(">") > 0) {
			symbol = ">";
			symbolIndex = formula.indexOf(">");
		} else if (formula.indexOf("<") > 0) {
			symbol = "<";
			symbolIndex = formula.indexOf("<");
		} else {
			throw new InterpreterException((new StringBuilder()).append(
					"IF\u903B\u8F91\u8868\u8FBE\u5F0F").append(formula).append(
					"\u8BED\u6CD5\u4E0D\u6B63\u786E\uFF01").toString());
		}
		double left = calculateFormula(formula.substring(0, symbolIndex));
		double right = calculateFormula(formula.substring(symbolIndex
				+ symbolLength));
		if (">".equals(symbol) && left > right || "<".equals(symbol)
				&& left < right || ">=".equals(symbol) && left >= right
				|| "<=".equals(symbol) && left <= right || "==".equals(symbol)
				&& left == right || "<>".equals(symbol) && left != right)
			return Boolean.valueOf(true);
		else
			return Boolean.valueOf(false);
	}

	private String[] splitFormula(String formula, char delima)
			throws InterpreterException {
		int blankets = 0;
		int subindex = 0;
		StringBuffer subbuf = new StringBuffer();
		StringBuffer subString = new StringBuffer();
		for (int i = 0; i < formula.length(); i++) {
			char charone = formula.charAt(i);
			if (charone == '(')
				blankets++;
			else if (charone == ')')
				blankets--;
			else if (charone == delima && blankets == 0) {
				subbuf.append(subString);
				subindex++;
				subbuf.append("\r");
				subString = new StringBuffer();
				if (subindex >= 8)
					throw new InterpreterException(
							"\u516C\u5F0F\u53C2\u6570\u6700\u591A\u4E0D\u80FD\u8D85\u8FC78\u4E2A\uFF01");
				continue;
			}
			subString = subString.append(charone);
		}

		if (subString != null)
			subbuf.append(subString);
		String subformula[] = subbuf.toString().split("\r");
		return subformula;
	}

	private double calculateBasic090414(double result, double result1, char oper)
			throws InterpreterException {
		switch (oper) {
		case 94: // '^'
			return pow(result, result1).doubleValue();

		case 42: // '*'
			return result * result1;

		case 43: // '+'
			return result + result1;

		case 45: // '-'
			return result - result1;

		case 47: // '/'
			if (result1 == 0.0D)
				return 0.0D;
			else
				return result / result1;
		}
		throw new InterpreterException((new StringBuilder()).append(
				"\u7B97\u672F\u8FD0\u7B97\u7B26").append(oper).append(
				"\u9519\u8BEF\uFF01").toString());
	}

	private int seperateFormula(String formula) throws InterpreterException {
		int blankets = 0;
		int subindex = 0;
		int subindexPow = 0;
		int subindexMD = 0;
		int subindexAS = 0;
		for (subindex = 0; subindex < formula.length(); subindex++) {
			char charone = formula.charAt(subindex);
			if (charone == '(') {
				blankets++;
				continue;
			}
			if (charone == ')') {
				blankets--;
				continue;
			}
			if (charone != '^' && charone != '*' && charone != '+'
					&& charone != '-' && charone != '/')
				continue;
			if (subindex == 0)
				throw new InterpreterException((new StringBuilder()).append(
						"\u5B50\u516C\u5F0F").append(formula).append(
						"\u7F3A\u5C11\u7B2C\u4E00\u4E2A\u53C2\u6570\uFF01")
						.toString());
			if (subindex == formula.length() - 1)
				throw new InterpreterException((new StringBuilder()).append(
						"\u5B50\u516C\u5F0F").append(formula).append(
						"\u7F3A\u5C11\u7B2C\u4E8C\u4E2A\u53C2\u6570\uFF01")
						.toString());
			if (blankets != 0)
				continue;
			if (charone == '+' || charone == '-') {
				subindexAS = subindex;
				continue;
			}
			if (charone == '*' || charone == '/') {
				subindexMD = subindex;
				continue;
			}
			if (charone == '^')
				subindexPow = subindex;
		}

		if (blankets > 0)
			throw new InterpreterException((new StringBuilder()).append(
					"\u5B50\u516C\u5F0F").append(formula).append(
					"\u7F3A\u5C11\u53F3\u62EC\u53F7')'\uFF01").toString());
		if (blankets < 0)
			throw new InterpreterException((new StringBuilder()).append(
					"\u5B50\u516C\u5F0F").append(formula).append(
					"\u7F3A\u5C11\u5DE6\u62EC\u53F7'('\uFF01").toString());
		if (subindexAS > 0)
			return subindexAS;
		if (subindexMD > 0)
			return subindexMD;
		if (subindexPow > 0)
			return subindexPow;
		else
			return 0;
	}

	public static String removeBrackets(String formula) {
		int blankets = 0;
		if (formula.length() <= 2 || formula.charAt(0) != '('
				|| formula.charAt(formula.length() - 1) != ')')
			return formula;
		for (int i = 1; i < formula.length() - 1; i++) {
			char charone = formula.charAt(i);
			if (charone == '(')
				blankets++;
			else if (charone == ')')
				blankets--;
			if (blankets < 0)
				return formula;
		}

		return removeBrackets(formula.substring(1, formula.length() - 1));
	}

	private Double getFieldValue(String field) throws InterpreterException {
		if (field.charAt(0) != 'F')
			return Double.valueOf(0.0D);
		if (planscore == null)
			return Double.valueOf(0.0D);
		if (field.charAt(1) != '1')
			return Double.valueOf(planscore.getPpsActualResult().doubleValue());
		if (planscore.getPpsSustain() == null)
			return Double.valueOf(0.0D);
		if (field.charAt(1) != '2')
			return Double.valueOf(planscore.getPpsTarget().doubleValue());
		if (planscore.getPpsTarget() == null)
			return Double.valueOf(0.0D);

		if (field.charAt(1) != '3')
			return Double.valueOf(planscore.getPpsChallenge().doubleValue());
		if (planscore.getPpsChallenge() == null)
			return Double.valueOf(0.0D);

		if (field.charAt(1) != '4')
			return new Double(field);

		if (planscore.getPpsActualResult() == null)
			return Double.valueOf(0.0D);
		try {
			return Double.valueOf(planscore.getPpsSustain().doubleValue());
		} catch (Exception e) {
			throw new InterpreterException((new StringBuilder()).append(
					"\u53D8\u91CF").append(field).append(
					"\u4E0D\u6B63\u786E\uFF01").toString());
		}

	}

	private static Double pow(double x, double n) throws InterpreterException {
		if (x == 0.0D)
			if (n == 0.0D)
				throw new InterpreterException("\u4E58\u5E42: 0^0 is undefined");
			else
				return Double.valueOf(0.0D);
		if (n < 0.0D)
			throw new InterpreterException("\u4E58\u5E42: Negative exponent");
		if (n == 0.0D)
			return Double.valueOf(1.0D);
		if (n % 2D == 0.0D)
			return pow(x * x, n / 2D);
		else
			return Double.valueOf(x * pow(x, n - 1.0D).doubleValue());
	}

	public int getValidateFlag() {
		return validateFlag;
	}

	public void setValidateFlag(int validateFlag) {
		this.validateFlag = validateFlag;
	}

	public Perfplanscore getPlanscore() {
		return planscore;
	}

	public void setPlanscore(Perfplanscore planscore) {
		this.planscore = planscore;
	}

	private int validateFlag;
	private Perfplanscore planscore;
}
